﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DataAccess;
using Entity;

namespace Controller
{
    public class CustomerController:ControllerBase
    {
        public IList<CustomerEntity> finAllCustomers()
        {
            IList<Customer> customers = (from c in Sdc.Customers
                                     select c).ToList();
            IList<CustomerEntity> customerEntities = new List<CustomerEntity>();
            foreach (var customer in customers)
            {
                CustomerEntity customerEntity = TransformCustomerToCustomerEntity(customer);
                customerEntities.Add(customerEntity);
            }
            return customerEntities;
        }
        public static CustomerEntity TransformCustomerToCustomerEntity(Customer customer)
        {
            CustomerEntity customerEntity=new CustomerEntity();
            customerEntity.Id = customer.Id;
            customerEntity.Name = customer.Name;
            customerEntity.ShortName = customer.ShortName;
            customerEntity.Address = customer.Address;
            customerEntity.ContactPerson = customer.ContactPerson;
            customerEntity.Phone = customer.Phone;
            customerEntity.Fax = customer.Fax;
            customerEntity.Email = customer.Email;
            customerEntity.ZipCode = customer.ZipCode;
            customerEntity.Number = customer.Number;
            customerEntity.Code = customer.Code;
            customerEntity.Remark = customer.Remark;
            return customerEntity;
        }

        public static Customer TransformCustomerEntityToCustomer(CustomerEntity customerEntity)
        {
            Customer customer = new Customer();
            customer.Id = customerEntity.Id;
            customer.Name = customerEntity.Name;
            customer.ShortName = customerEntity.ShortName;
            customer.Address = customerEntity.Address;
            customer.ContactPerson = customerEntity.ContactPerson;
            customer.Phone = customerEntity.Phone;
            customer.Fax = customerEntity.Fax;
            customer.Email = customerEntity.Email;
            customer.ZipCode = customerEntity.ZipCode;
            customer.Number = customerEntity.Number;
            customer.Code = customerEntity.Code;
            customer.Remark = customerEntity.Remark;
            return customer;
        }

        public CustomerEntity FindCustomerByCustomerId(int p)
        {
            Customer customer = (from c in Sdc.Customers
                                 where c.Id == p
                                 select c).SingleOrDefault();
            CustomerEntity customerEntity = TransformCustomerToCustomerEntity(customer);
            return customerEntity;
        }

        public bool AddCustomer(CustomerEntity customerEntity)
        {
            try
            {
                Customer customer = TransformCustomerEntityToCustomer(customerEntity);
                Sdc.Customers.InsertOnSubmit(customer);
                Sdc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool UpdateCustomer(CustomerEntity customerEntity)
        {
            try
            {
                Customer customer = (from c in Sdc.Customers where c.Id == customerEntity.Id select c).SingleOrDefault();
                customer.Name = customerEntity.Name;
                customer.ShortName = customerEntity.ShortName;
                customer.Phone = customerEntity.Phone;
                customer.Fax = customerEntity.Fax;
                customer.Email = customerEntity.Email;
                customer.ContactPerson = customerEntity.ContactPerson;
                customer.Address = customerEntity.Address;
                customer.ZipCode = customerEntity.ZipCode;
                customer.Number = customerEntity.Number;
                customer.Code = customerEntity.Code;
                customer.Remark = customerEntity.Remark;

                Sdc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        public bool DeleteCustomerById(int customerId)
        {
            try
            {
                Customer customer = (from c in Sdc.Customers where c.Id == customerId select c).SingleOrDefault();
                Sdc.Customers.DeleteOnSubmit(customer);
                Sdc.SubmitChanges();
                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
